﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IBMS.Common;

namespace IBMS.Page
{
    public partial class ProjectRegister : System.Web.UI.Page
    {
        private DB db = new DB();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Div1.Visible = true;
            this.Div2.Visible = false;
            
        }

        protected void btn_zhuce_Click(object sender, EventArgs e)
        {
            string id = this.id.Text.ToString();
            string name = this.name.Text.ToString();
            string version = this.version.Text.ToString();
            string sonSys = this.sonSys.Text.ToString();

            bool result = db.ExecuteNonQury("insert into Project(ProjectId,Name,Version) values('"+id+"','"+name+"','"+version+"')");
            if (result)
            {
                string[] son=sonSys.Split(new char[]{';'});
                for (int i = 0; i < son.Length; i++)
                {
                    result = db.ExecuteNonQury("insert into SonSystem(Father,Name)  values('"+id+"','"+son[i]+"')");
                }
            }
            if (result)
            {
                for (int i = 0; i < this.DropDownList1.Items.Count-1; i++)
                {
                    DataTable d1 = new DataTable();
                    d1 = db.GetDataTable("select * from Users where Name='"+this.DropDownList1.Items[i].Value+"'");
                    string username = d1.Rows[0]["Name"].ToString();
                    string oldProjectId = d1.Rows[0]["ProjectId"].ToString();
                    result = db.ExecuteNonQury("update Users set ProjectId='"+id+";"+oldProjectId+"' where Name='"+username+"'");
                }
            }
            if (result)
            {
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功'); window.close()", true);
            }
            else
            {
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('操作失败'); window.location='ProjectRegister.aspx'", true);
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DropDownList1.SelectedItem.Value == "点击选择")
            {
                this.Div2.Visible = true;
                this.Div1.Visible = false;
                this.CheckBoxList1.Items.Clear();
                DataTable datatable = new DataTable();
                datatable = db.GetDataTable("select * from Users");
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    ListItem cb = new ListItem();
                    cb.Text = datatable.Rows[i]["Name"].ToString();
                    cb.Value = datatable.Rows[i]["Name"].ToString();
                    this.CheckBoxList1.Items.Add(cb);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.DropDownList1.Items.Clear();
            for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
            {
                if (this.CheckBoxList1.Items[i].Selected)
                {
                    
                    ListItem item=new ListItem();
                    item.Text = this.CheckBoxList1.Items[i].Value;
                    item.Value = this.CheckBoxList1.Items[i].Value;
                    this.DropDownList1.Items.Add(item);
                }
            }
            ListItem ii = new ListItem();
            ii.Text = "点击选择";
            ii.Value = "点击选择";
            this.DropDownList1.Items.Add(ii);
        }
    }
}